#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPixmap>
#include <QTimer>
#include <QPaintEvent>
#include <QPainter>
#include <QPoint>
#include <QRect>
#include <bitset>
#include <string>
#include <cstring>
#include <filesystem>

#include <windows.h>

#include "background.h"
#include "move_object.h"
#include "bomb.h"
#include "fps.h"
#include "config.h"
#include "sound.h"

using std::bitset;
using std::string;
using namespace std::filesystem;

enum class Direct { up, down, left, right };

struct Quit_error {};       // 按Q键关闭游戏

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

signals:
    void help();        // 显示游戏帮助的信号

protected:
    void paintEvent(QPaintEvent* event);

    void keyPressEvent(QKeyEvent* event);

    void keyReleaseEvent(QKeyEvent* event);

    void mouseMoveEvent(QMouseEvent* event);

    void mousePressEvent(QMouseEvent* event);

    void mouseReleaseEvent(QMouseEvent* event);

private slots:
    void frame();       // 每一帧

    void start_game();      // 重新开始这一关

private:
    Ui::MainWindow *ui;
    Map::Background bg {1};
    void set_level(int lev);        // 重置为第几关。lev必须为有效的关卡
    QTimer timer;
    QPixmap snapshot;
    void update_snapshot();     // 更新快照
    QList<Object::Bullet> bullets;
    QList<Object::Bullet> enemy_bullets;
    QList<Object::Enemy> enemys;
    Object::Player* player;
    State state {State::free};
    State chosen_state {default_state};
    State pause_state;
    void set_enemy();       // 生成敌人
    void update_enemys();       // 更新敌人
    static void update_enemy_bullets(QList<Object::Bullet>& enemy_bullets);     // 更新敌人子弹
    static void update_bullets(QList<Object::Bullet>& bullets);     // 更新自机子弹
    static void update_bombs(QList<Bomb::Bomb>& bombs);     // 更新爆炸
    QList<Bomb::Bomb> bombs;
    void enemys_draw(QPixmap* pix);     // 绘制敌人
    void bullets_e_draw(QPixmap* pix);      // 绘制敌人子弹
    void bullets_draw(QPixmap* pix);        // 绘制子弹
    bitset<4> directs;
    bool shoot {false};
    void draw_condition(QPixmap* pix);      // 绘制状态信息
    Object::Boss* boss;
    int boss_interval {boss_quit};
    bool boss_action();     // Boss行动！返回true表示Boss寄了
    Fps::Fps fps;
    void pause();       // 暂停游戏
    Sound::Bgm bgm;
    void guide();       // 打开教程
    int resurrection;
};

#endif // MAINWINDOW_H
